package com.rtx.edi.platform.express.auth.controller;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.express.auth.dto.PlAuthShopDto;
import com.rtx.edi.model.express.auth.po.PlAuthShop;
import com.rtx.edi.model.express.auth.po.PlAuthShopApp;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.express.auth.service.PlAuthShopAppService;
import com.rtx.edi.platform.express.auth.service.PlAuthShopService;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 店铺信息 
 * @author luoqiongli
 *
 */
@Api(tags = {"店铺信息"})
@RestController
@RequestMapping("/v1/plAuthShop")
public class PlAuthShopController {
	
	@Autowired
	private PlAuthShopService plAuthShopService;
	
	@Autowired
	private PlAuthShopAppService plAuthShopAppService;
	
	@ApiOperation(value = "店铺信息列表", notes = "分页获取店铺信息列表")
    @PostMapping("qryPlAuthShopList")
	public EdiResponse qryPlAuthShop(@RequestBody PlAuthShopDto dto) {
		PlAuthShop shop =new PlAuthShop();
		BeanUtils.copyProperties(dto,shop);
		Page<PlAuthShop> pageParam = new Page<>(dto.getPageNum(),dto.getPageSize());
		plAuthShopService.selectPlAuthShop(pageParam,shop);
		return EdiResponse.success().data("page",pageParam);
	}
	
	@ApiOperation(value = "查询店铺信息列表", notes = "查询店铺信息列表ById")
    @PostMapping("qryPlAuthShopById")
	public EdiResponse qryPlAuthShopById(@RequestBody PlAuthShopDto dto) {
		
		return EdiResponse.success().data("item",plAuthShopService.getById(dto.getId()));
	}
	
	@ApiOperation(value = "新增店铺信息", notes = "新增店铺信息")
    @PostMapping("addPlAuthShop")
	public EdiResponse addPlAuthShop(@RequestBody PlAuthShopDto dto) {
		checkField(dto);
		QueryWrapper<PlAuthShop> qw =new QueryWrapper<PlAuthShop>();
		qw.eq("name", dto.getName())
		.eq("deleteflag","0")
		.eq("shopid", dto.getShopid());
		boolean exists=plAuthShopService.getBaseMapper().exists(qw);
		if(exists) {
			return EdiResponse.failed().returnMgr("店铺信息已存在不允许重复创建");
		}
		PlAuthShop shop= new PlAuthShop();
		BeanUtils.copyProperties(dto,shop);
		shop.setAddwho("ris_edi_dev");
		shop.setEditwho("ris_edi_dev");
		return plAuthShopService.addPlAuthShop(shop)?EdiResponse.success():EdiResponse.failed();
		
	}
	
	@ApiOperation(value = "修改店铺信息", notes = "修改店铺信息")
    @PostMapping("udpPlAuthShop")
	public EdiResponse udpPlAuthShop(@RequestBody PlAuthShopDto dto) {
		checkField(dto);
		QueryWrapper<PlAuthShopApp> qw=new QueryWrapper<PlAuthShopApp>();
		qw.eq("shopid", dto.getId());
		qw.eq("active", "1");
		boolean flag=plAuthShopAppService.getBaseMapper().exists(qw);
		if(flag) {
			return EdiResponse.failed().returnMgr("绑定表中已经存在且被启用 不允许修改");
		}
		PlAuthShop shop= new PlAuthShop();
		BeanUtils.copyProperties(dto,shop);
		shop.setEditwho("ris_edi_dev");
		shop.setEditdate(LocalDateTime.now());
		return plAuthShopService.updateById(shop)?EdiResponse.success():EdiResponse.failed();
	}
	
	@ApiOperation(value = "批量删除店铺信息")
    @PostMapping("delPlAuthShopByIds")
	public EdiResponse delPlAuthShopByIds(@RequestBody List<PlAuthShopDto> list) {
		try {
			
			// plAuthShopService.delPlAuthShops(list);
			// 逻辑删除 
			for (PlAuthShopDto dto : list) {
				QueryWrapper<PlAuthShopApp> qw=new QueryWrapper<PlAuthShopApp>();
				qw.eq("shopid", dto.getId());
				qw.eq("active", "1");
				boolean flag=plAuthShopAppService.getBaseMapper().exists(qw);
				if(flag) {
					return EdiResponse.failed().returnMgr("绑定表中已经存在且被启用 不允许修改");
				}
				PlAuthShop shop= new PlAuthShop();
				BeanUtils.copyProperties(dto,shop);
				shop.setEditwho("ris_edi_dev");
				shop.setEditdate(LocalDateTime.now());
				shop.setDeleteflag("1");
				plAuthShopService.updateById(shop);
			}
			return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
		
	}
	
	@ApiOperation(value = "删除店铺信息")
    @PostMapping("delPlAuthShop")
	public EdiResponse delPlAuthShop(@RequestBody PlAuthShopDto dto) {
		
		
		return plAuthShopService.removeById(dto.getId())?EdiResponse.success():EdiResponse.failed();
		
		
	}
	
	/**
	 * 校验
	 * @param shop
	 */
	private void checkField(PlAuthShopDto dto) {
		 Assert.notNull(dto.getName(),"店铺名称不能为空");
		 Assert.notNull(dto.getShopid(),"店铺Id不能为空");
	}

}
